package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectTest {

  public static void main(String[] args) {
    //

    Class<?> classBook = null;
    //    try {
    //      classBook = Class.forName("reflect.Book");
    //      Object objectBook = classBook.newInstance();
    //      Book book = (Book) objectBook;
    //      book.setBookCoum(6666);
    //      System.out.println(book.getBookCoum());
    //    } catch (ClassNotFoundException e) {
    //      e.printStackTrace();
    //    } catch (IllegalAccessException e) {
    //      e.printStackTrace();
    //    } catch (InstantiationException e) {
    //      e.printStackTrace();
    //    }

    try {
      classBook = Class.forName("reflect.Book");
      Constructor con = classBook.getDeclaredConstructor(int.class);
      con.setAccessible(true);
      Method me = classBook.getDeclaredMethod("setBookCoum", int.class);
      Book book = (Book) con.newInstance(77777);
      me.invoke(book, 9999);
      Method me2 = classBook.getMethod("getBookCoum", null);

      System.out.println("getBookCoum:" + me2.invoke(book, null));
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
  }
}
